import 'package:flutter/material.dart';
import 'package:moonmate/theme/app_theme.dart';

class PeriodSelectionDialog extends StatefulWidget {
  final bool isStartDate;
  final DateTime initialDate;
  
  const PeriodSelectionDialog({
    super.key,
    required this.isStartDate,
    required this.initialDate,
  });

  @override
  State<PeriodSelectionDialog> createState() => _PeriodSelectionDialogState();
}

class _PeriodSelectionDialogState extends State<PeriodSelectionDialog> {
  late DateTime selectedDate;
  List<DateTime> dateOptions = [];
  
  @override
  void initState() {
    super.initState();
    selectedDate = widget.initialDate;
    _generateDateOptions();
  }
  
  void _generateDateOptions() {
    
    dateOptions = [];
    final now = DateTime.now();
    
    for (int i = -30; i <= 7; i++) {
      dateOptions.add(DateTime(now.year, now.month, now.day).add(Duration(days: i)));
    }
  }
  
  String _getDisplayText(DateTime date) {
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);
    final yesterday = today.subtract(const Duration(days: 1));
    final tomorrow = today.add(const Duration(days: 1));
    
    if (date.isAtSameMomentAs(today)) {
      return '(今天)';
    } else if (date.isAtSameMomentAs(yesterday)) {
      return '(昨天)';
    } else if (date.isAtSameMomentAs(tomorrow)) {
      return '(明天)';
    } else {
      
      final difference = date.difference(today).inDays;
      String relativeDay;
      
      if (difference > 0) {
        relativeDay = '$difference天后';
      } else {
        relativeDay = '${-difference}天前';
      }
      
      return '($relativeDay)';
    }
  }
  
  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(16),
      ),
      child: Container(
        padding: const EdgeInsets.all(16),
        constraints: const BoxConstraints(maxWidth: 350),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                TextButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: const Text('取消', style: TextStyle(fontSize: 16)),
                ),
                Text(
                  widget.isStartDate ? '选择经期开始日期' : '选择经期结束日期',
                  style: const TextStyle(fontSize: 17, fontWeight: FontWeight.w600),
                ),
                TextButton(
                  onPressed: () {
                    Navigator.pop(context, selectedDate);
                  },
                  child: const Text('确定', style: TextStyle(color: AppTheme.primaryColor, fontSize: 16)),
                ),
              ],
            ),
            
            const SizedBox(height: 16),
            
            
            Container(
              height: 250,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(12),
                color: Colors.grey[50],
              ),
              child: ListView.builder(
                itemCount: dateOptions.length,
                itemBuilder: (context, index) {
                  final date = dateOptions[index];
                  final isSelected = date.year == selectedDate.year &&
                                    date.month == selectedDate.month &&
                                    date.day == selectedDate.day;
                  
                  return GestureDetector(
                    onTap: () {
                      setState(() {
                        selectedDate = date;
                      });
                    },
                    child: Container(
                      padding: const EdgeInsets.symmetric(vertical: 14, horizontal: 20),
                      margin: const EdgeInsets.symmetric(vertical: 2),
                      decoration: isSelected
                          ? BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(8),
                              boxShadow: [
                                BoxShadow(
                                  color: Colors.grey.withValues(alpha: 0.2),
                                  spreadRadius: 1,
                                  blurRadius: 3,
                                ),
                              ],
                            )
                          : null,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            '${date.year}年 ${date.month}月 ${date.day}日${_getDisplayText(date)}',
                            style: TextStyle(
                              fontSize: 16,
                              color: isSelected ? AppTheme.primaryColor : Colors.black87,
                              fontWeight: isSelected ? FontWeight.w500 : FontWeight.normal,
                            ),
                          ),
                          if (isSelected)
                            const Icon(Icons.check_circle, color: AppTheme.primaryColor),
                        ],
                      ),
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}